home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / winftp.zip / WNFTPSRC.ZIP / WS_LOCAL.C < prev    next >
C/C++ Source or Header  |  1994-01-11  |  17KB  |  541 lines

  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <dos.h>
  4. #include <errno.h>
  5.  
  6. #include "ws_glob.h"
  7. #include "winftp.h"
  8.  
  9. #ifdef WIN32
  10.  
  11. void CreateUniqueName (LPSTR localname, LPSTR szName, LPSTR Ext)
  12. {
  13.   int nIndex;
  14.   char szUniq[_MAX_PATH];
  15.    
  16.     nIndex = 0;
  17.     lstrcpy (szUniq, localname);
  18.     while ((GetFileAttributes (szUniq)!=0xFFFFFFFF) && (nIndex<99))
  19.     {
  20.       DoPrintf ("[recvuniq] %s - %s - %s", szName, Ext, szUniq);
  21.       switch (Ext[0])
  22.       {
  23.         case '\0': wsprintf (szUniq, "%s.%03u", szName, nIndex); break;
  24.         default  : if (lstrlen (szName)>5) szName[5]='\0';
  25.                    wsprintf (szUniq, "%s%3.3d.%s", szName, nIndex, Ext);
  26.       }
  27.       nIndex++;
  28.     }
  29.     lstrcpy (localname, szUniq);
  30. }
  31.  
  32. #else
  33.  
  34. void CreateUniqueName (LPSTR localname, LPSTR szName, LPSTR Ext)
  35. {
  36.   int nIndex;
  37.   char szUniq[80];
  38.   struct _find_t f;
  39.     
  40.     nIndex = 0;
  41.     lstrcpy (szUniq, localname);
  42.     while ((int) _dos_findfirst (szUniq, _A_NORMAL, &f)==0 && nIndex<99) 
  43.     {
  44.       DoPrintf ("[recvuniq] %s - %s - %s", szName, Ext, szUniq);
  45.       switch (Ext[0])
  46.       {
  47.         case '\0': wsprintf (szUniq, "%s.%03u", szName, nIndex); break;
  48.         default  : if (lstrlen (szName)>5) szName[5]='\0';
  49.                    wsprintf (szUniq, "%s%3.3d.%s", szName, nIndex, Ext);
  50.       }
  51.       nIndex++;
  52.     }
  53.     lstrcpy (localname, szUniq);
  54. }
  55.  
  56. #endif
  57.  
  58. //*****************************************************************************
  59. //*****************************************************************************
  60. BOOL OnCmdInitHostList (HWND hDlg, WPARAM wParam, LPARAM lParam)
  61. {
  62.   int nI;
  63.   char szHost[40];
  64.   char szType[30];
  65.   char szBuf[80];
  66.  
  67.   for (nI=0; nI<nCfgNum; nI++)
  68.   {
  69.       lstrcpy (szHost, GetHostName (nI));
  70.       if (lstrlen (szHost)>0)
  71.       {
  72.         lstrcpy (szType, GetHostType (nI));
  73.         wsprintf (szBuf, "%-30s  %s", szHost, szType);
  74.         SendDlgItemMessage (hDlg, LBX_HOSTLIST, LB_ADDSTRING, (WPARAM) 0, (LPARAM)(LPCSTR) szBuf);
  75.       }
  76.   }
  77.   if (bConnected)
  78.   {
  79.     lstrcpy (szType, GetHostTypeValue (nHostType));
  80.     wsprintf (szBuf, "Connected Host Type is %s", szType);
  81.     SetDlgItemText (hDlg, TXT_CURHOST, szBuf);
  82.   }
  83.   else
  84.   {
  85.     SetDlgItemText (hDlg, TXT_CURHOST, "Not Connected to Remote Host");
  86.   }
  87.   SendDlgItemMessage (hDlg, LBX_HOSTLIST, WM_SETFONT, 
  88.       (WPARAM) GetStockObject (ANSI_FIXED_FONT), (LPARAM) MAKELONG (TRUE, 0));
  89.   return TRUE;
  90. }
  91.  
  92. //*****************************************************************************
  93. //*****************************************************************************
  94. BOOL OnCmdHostListCmd (HWND hDlg, WPARAM wParam, LPARAM lParam)
  95. {
  96.   WORD wNotifyCode;
  97.   WORD wCtlID;
  98.  
  99. #ifdef WIN32
  100.   wNotifyCode = HIWORD (wParam);
  101.   wCtlID      = LOWORD (wParam);
  102. #else
  103.   wNotifyCode = HIWORD (lParam);
  104.   wCtlID      = wParam;
  105. #endif
  106.   
  107.   switch (wCtlID)
  108.   {
  109.     case IDOK : EndDialog (hDlg, TRUE);
  110.                 return TRUE;
  111.     default   : return FALSE;
  112.   }
  113.   return FALSE;
  114. }
  115.  
  116.  
  117. //*****************************************************************************
  118. //*****************************************************************************
  119. BOOL CALLBACK WS_HostListProc (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  120. {
  121.   switch (Msg)
  122.   {
  123.     case WM_INITDIALOG: return OnCmdInitHostList (hDlg, wParam, lParam); 
  124.     case WM_COMMAND   : return OnCmdHostListCmd (hDlg, wParam, lParam);
  125.     default           : return FALSE;
  126.   }
  127.   return FALSE;
  128. }
  129.  
  130. //*****************************************************************************
  131. //*****************************************************************************
  132. OnCmdDisplayHostList (HWND hWnd, WORD wCtlID, WORD wNotifyCode)
  133. {
  134.   FARPROC lpfnMsgProc;
  135.   
  136.   lpfnMsgProc = MakeProcInstance ((FARPROC) WS_HostListProc, hInst);
  137.   DialogBox (hInst, (LPSTR) "DLG_HOSTLIST", hWnd, lpfnMsgProc);
  138.   FreeProcInstance (lpfnMsgProc);
  139.   return 0;
  140. }
  141.  
  142. //*****************************************************************************
  143. //*****************************************************************************
  144. OnCmdDropFiles (HWND hWnd, LPSTR *lpPtr)
  145. {
  146.   char szRemoteName[_MAX_FNAME+_MAX_EXT+2], szExt[_MAX_EXT];
  147.   int nCount, nRC, nIndex;
  148.   u_char szTmp[150];
  149.  
  150.   if (!bConnected) return 0;
  151.   if (lpPtr==NULL) return 0;
  152.   if (lpPtr[0]==NULL) return 0;
  153.   
  154.   nCount=0; 
  155.   while (lpPtr[nCount]!=NULL) nCount++;
  156.   bCancelXfer = FALSE;
  157.   bOpInProgress = TRUE;
  158.   CreateXferWindow();
  159.   for (nIndex=0; (nIndex<nCount)&(!bCancelXfer); nIndex++) 
  160.   {
  161.     if (lpPtr[nIndex]!=NULL)
  162.     {
  163.       _splitpath (lpPtr[nIndex], NULL, NULL, szRemoteName, szExt);
  164.       lstrcat (szRemoteName, szExt);
  165.       if (bInteractive)
  166.       {
  167.         FARPROC lpfnMsgProc;
  168.       
  169.         wsprintf (szDlgPrompt, "Enter remote file name for %s:", lpPtr[nIndex]);
  170.         lstrcpy (szDlgEdit, szRemoteName);
  171.         lpfnMsgProc = MakeProcInstance ((FARPROC) WS_InputMsgProc, hInst);
  172.         DialogBox (hInst, (LPSTR) "DLG_INPUT", hWnd, lpfnMsgProc);
  173.         FreeProcInstance (lpfnMsgProc);
  174.         lstrcpy (szRemoteName, szDlgEdit);
  175.       }
  176.       DoPrintf (szTmp, "Sending %s as %s (%u of %u)", lpPtr[nIndex], szRemoteName, nIndex+1, nCount);
  177.       wsprintf (szTmp, "STOR %s", szRemoteName);
  178.       nRC = SendFile (ctrl_socket, szTmp, lpPtr[nIndex], fType);
  179.       GlobalFreePtr (lpPtr[nIndex]);
  180.     }
  181.   }
  182.   DeleteXferWindow();
  183.   GetRemoteDirForWnd (hWnd);
  184.   bOpInProgress = FALSE;
  185.   GlobalFreePtr (lpPtr);
  186.   return 0;
  187. }
  188.  
  189. /************************************************************************/
  190. /************************************************************************/
  191. DoRunScript (HWND hWnd, LPSTR lpScript)
  192. {
  193.   FILE *fp;
  194.   char szCmd[150];
  195.   LPSTR lpParm;
  196.   
  197.   if ((fp=fopen (lpScript, "rt"))==NULL)
  198.   {
  199.     DoPrintf ("Could not execute Script: %s", lpScript);
  200.     return 0;
  201.   }
  202.   while (fgets (szCmd, 145, fp)!=NULL)
  203.   {
  204.     strtok (szCmd, "\n");
  205.     lpParm = strchr (szCmd, ' ');
  206.     if (lpParm!=NULL) *lpParm++ = '\0';
  207.     if (lstrcmpi (szCmd, "CMD") == 0) command (ctrl_socket, lpParm);
  208.   }
  209.   fclose (fp);
  210.   return 0;
  211. }
  212.  
  213. static LONG lTotalBytes=0, lXferBytes=0;
  214. static int  nPercentXfer;
  215. static HWND hWndXfer=NULL;
  216.  
  217. /************************************************************************/
  218. /************************************************************************/
  219. void PaintXfer (HDC hDC)
  220. {
  221.   RECT rc;
  222.   char szBuf[10];
  223.  
  224.   CreateButtonPens();
  225.   SelectObject (hDC, GetStockObject (WHITE_BRUSH));
  226.   Rectangle (hDC, (nWndx*20)/4, (nWndy*10)/8, (nWndx*122)/4, (nWndy*20)/8);
  227.   rc.left = (nWndx*21)/4;  rc.right = (nWndx*(nPercentXfer+21))/4;
  228.   rc.top  = (nWndy*11)/8;  rc.bottom= (nWndy*19)/8;
  229.   FillRect (hDC, &rc, GetStockObject (GRAY_BRUSH));
  230.   rc.right = (nWndx*121)/4;
  231.   SetBkMode (hDC, TRANSPARENT);
  232.   wsprintf (szBuf, "%d%%", nPercentXfer);
  233.   DrawText (hDC, szBuf, lstrlen (szBuf), &rc, DT_CENTER | DT_VCENTER);
  234.   SetBkMode (hDC, OPAQUE);
  235.   BoxIt (hDC, 18, 8, 106, 14, FALSE);
  236.   DeleteButtonPens();
  237. }
  238.  
  239. /************************************************************************/
  240. /************************************************************************/
  241. void OnPaintXfer (HWND hWnd)
  242. {
  243.   HDC hDC;
  244.   PAINTSTRUCT ps;
  245.   
  246.   hDC = BeginPaint (hWnd, &ps);
  247.   PaintXfer (hDC);
  248.   EndPaint (hWnd, &ps);
  249. }
  250.  
  251. /************************************************************************/
  252. /************************************************************************/
  253. void ForcePaintXfer (HWND hWnd)
  254. {
  255. }
  256.  
  257. /************************************************************************/
  258. /************************************************************************/
  259. void SetTotalBytes (LONG lBytes)
  260. {
  261.   lTotalBytes = lBytes;
  262. }
  263.  
  264. /************************************************************************/
  265. /************************************************************************/
  266. void SetXmitBytes (LONG lBytes)
  267. {
  268.   int nOld;
  269.   
  270.   lXferBytes = lBytes;
  271.   if ((lTotalBytes==0)||(hWndXfer==NULL)) return;
  272.   nOld = nPercentXfer;
  273.   nPercentXfer = (int) ((lBytes*100)/lTotalBytes);
  274.   if (nOld!=nPercentXfer) 
  275.   {
  276.     HDC hDC;
  277.   
  278.     hDC = GetDC (hWndXfer);
  279.     PaintXfer (hDC);
  280.     ReleaseDC (hWndXfer, hDC);
  281.   }
  282. }
  283.  
  284. /************************************************************************/
  285. /************************************************************************/
  286. LRESULT CALLBACK WndXferProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  287. {
  288.   switch (Msg)
  289.   {
  290.     case WM_GETMINMAXINFO: 
  291.     {
  292.       MINMAXINFO FAR *lpmmi=(MINMAXINFO FAR *) lParam;
  293.       lpmmi->ptMaxSize.x = (nWndx*140)/4;
  294.       lpmmi->ptMaxSize.y = (nWndy*45)/8;
  295.       lpmmi->ptMaxTrackSize.x = (nWndx*140)/4;
  296.       lpmmi->ptMaxTrackSize.y = (nWndy*45)/8;
  297.       lpmmi->ptMinTrackSize.x = (nWndx*140)/4;
  298.       lpmmi->ptMinTrackSize.y = (nWndy*45)/8;
  299.     } break;
  300.     case WM_PAINT: OnPaintXfer (hWnd); return 0L;
  301.   }
  302.   return (LRESULT) DefWindowProc (hWnd, Msg, wParam, lParam);
  303. }
  304.  
  305. /************************************************************************/
  306. /************************************************************************/
  307. void CreateXferWindow()
  308. {
  309.   if (hWndXfer!=NULL) return;
  310.   hWndXfer = CreateWindowEx ( WS_EX_TOPMOST, 
  311.        szXferWnd, "",  WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CAPTION,
  312.        CW_USEDEFAULT, CW_USEDEFAULT, (nWndx*140)/4, (nWndy*40)/8, (HWND) NULL, 
  313.        (HMENU) NULL, hInst,  NULL);
  314.   lTotalBytes = 0L;
  315.   lXferBytes  = 0L;
  316.   nPercentXfer = 0L;
  317. }
  318.  
  319. /************************************************************************/
  320. /************************************************************************/
  321. void DeleteXferWindow()
  322. {
  323.   if (hWndXfer==NULL) return;
  324.   DestroyWindow (hWndXfer);
  325.   lTotalBytes  = lXferBytes = 0l;
  326.   nPercentXfer = 0;
  327.   hWndXfer = NULL;
  328. }
  329.  
  330. /************************************************************************/
  331. /************************************************************************/
  332. void SetXferWindowText (LPSTR lpStr)
  333. {
  334.   SetWindowText (hWndXfer, lpStr);
  335. }
  336.  
  337. static HWND hWndViewMsg = (HWND) NULL;
  338. static LPSTR *lpMsg;
  339. static int nMaxMsg, nCurMsg, nVertPos, nHorzPos, nHorzMax;
  340.  
  341. static int nHorzTab = 20;
  342. static int nHorzSiz, nVertSiz;
  343. static LPSTR szNoMem = "Unable to allocate memory!";
  344. static LPSTR szZoomErr = "Zoom Error";
  345. static BOOL bZoomFlag=FALSE;
  346.  
  347. //*****************************************************************************
  348. //*****************************************************************************
  349. OnCmdZoomInfo (HWND hWnd)
  350. {
  351.   int nI, nMax;
  352.   LPSTR lp;
  353.   
  354.   if (GetStatusLine (0)==NULL) return 0;
  355.   switch (bZoomFlag)
  356.   {
  357.     case FALSE: bZoomFlag = TRUE;
  358.                 ClearZoomInfo();
  359.                 nMax = GetMaxStatusLines();
  360.                 for (nI=0; nI<nMax; nI++)
  361.                 {
  362.                   lp = GetStatusLine (nI);
  363.                   if (lstrlen (lp) > 0) AddMessageToView (lp, "WinFTP: Zoom WinDow");
  364.                 } break;
  365.     case TRUE : bZoomFlag = FALSE;
  366.                 SendMessage (hWndViewMsg, WM_CLOSE, 0, 0L);
  367.   }
  368.   return 0;
  369. }
  370.  
  371. //*************************************************************************
  372. //*************************************************************************
  373. void AddMessageToView (LPSTR lpBuf, LPSTR lpHdr)
  374. {
  375.   if (lpBuf==NULL) return;
  376.   if (lpBuf[0]=='[') return;
  377.   nMaxMsg = 150; 
  378.   if (lpMsg==NULL)
  379.   {
  380.     lpMsg = (LPSTR *) GlobalAllocPtr (GHND, nMaxMsg * sizeof (LPSTR));
  381.     nCurMsg = 0;
  382.   }
  383.   if (lpMsg==NULL) 
  384.   {
  385.     MessageBox (hWndMain, szNoMem, szZoomErr, MB_OK);
  386.     return;
  387.   }
  388.   if (hWndViewMsg==(HWND) NULL) hWndViewMsg = CreateWindow (szMsgWnd, lpHdr,  
  389.        WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE, 
  390.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
  391.          (HWND) NULL, (HMENU) NULL, hInst, NULL);
  392.  
  393.   if ((hWndViewMsg!=(HWND) NULL))
  394.   {
  395.     LPSTR lp;
  396.     
  397.     if (nCurMsg>=nMaxMsg)
  398.     {
  399.       GlobalFreePtr (lpMsg[0]);
  400.       memmove (lpMsg, lpMsg+1, (nMaxMsg-1)*sizeof (LPSTR));
  401.       nCurMsg = nMaxMsg-1;
  402.     }
  403.     lp = lpMsg[nCurMsg] = (LPSTR) GlobalAllocPtr (GHND, lstrlen (lpBuf) + 5);
  404.     if (lp!=NULL) 
  405.     {
  406.       HDC hDC;
  407.       SIZE siz;
  408.       
  409.       lstrcpy (lp, lpBuf);
  410.       hDC = GetDC (hWndViewMsg);
  411.       GetTextExtentPoint (hDC, lp, lstrlen (lp), &siz);
  412.       ReleaseDC (hWndViewMsg, hDC);
  413.       nHorzMax = __max (nHorzMax, siz.cx);
  414.       SetScrollRange (hWndViewMsg, SB_HORZ, 0, nHorzMax, TRUE);
  415.       SetScrollRange (hWndViewMsg, SB_VERT, 0, nCurMsg, TRUE);
  416.       InvalidateRect (hWndViewMsg, NULL, TRUE);
  417.       nCurMsg++;
  418.     }
  419.     else 
  420.     {
  421.       MessageBox (hWndMain, szNoMem, szZoomErr, MB_OK);
  422.       return;
  423.     }
  424.   }
  425. }
  426.  
  427. //*************************************************************************
  428. //*************************************************************************
  429. void ClearZoomInfo()
  430. {
  431.   int nI;
  432.   
  433.   if (lpMsg!=NULL) 
  434.   {
  435.     for (nI=0; nI<nMaxMsg; nI++) 
  436.     {
  437.       if (lpMsg[nI]!=NULL) GlobalFreePtr (lpMsg[nI]);
  438.       lpMsg[nI] = NULL;
  439.     }
  440.   }
  441.   nCurMsg = nVertPos = nHorzPos = nHorzMax = 0;
  442.   SetScrollPos (hWndViewMsg, SB_HORZ, 0, TRUE);
  443.   SetScrollPos (hWndViewMsg, SB_VERT, 0, TRUE);
  444.   SetScrollRange (hWndViewMsg, SB_HORZ, 0, 1, TRUE);
  445.   SetScrollRange (hWndViewMsg, SB_VERT, 0, 1, TRUE);
  446. }
  447.  
  448. //*************************************************************************
  449. //*************************************************************************
  450. void ReleaseMsgMem()
  451. {
  452.   int nI;
  453.   
  454.   if (lpMsg==NULL) return;
  455.   for (nI=0; nI<nMaxMsg; nI++) if (lpMsg[nI]!=NULL) GlobalFreePtr (lpMsg[nI]);
  456.   GlobalFreePtr (lpMsg);
  457.   lpMsg = NULL;
  458. }
  459.  
  460. //*************************************************************************
  461. //*************************************************************************
  462. LRESULT PaintViewMessage (HWND hWnd, int nVertPos)
  463. {
  464.   HDC         hDC;
  465.   PAINTSTRUCT ps;
  466.   int         nI, nRows, nLine, nPos, nHt;
  467.   RECT        rRect;
  468.   TEXTMETRIC  tm;
  469.  
  470.   hDC = BeginPaint (hWnd, &ps);
  471.   GetTextMetrics (hDC,&tm);
  472.   nHt = tm.tmHeight + tm.tmExternalLeading;
  473.   GetClientRect (hWnd, &rRect);
  474.   nHorzSiz = rRect.right;
  475.   nVertSiz = nRows = rRect.bottom/nHt;
  476.   ShowScrollBar (hWnd, SB_VERT, (nRows<nCurMsg));
  477.   nLine = nVertPos;
  478.   if (lpMsg!=NULL)
  479.   {
  480.     for (nI=nPos=0; (nI<nRows) && (nLine<nCurMsg); nI++, nLine++, nPos+=nHt) 
  481.     {
  482.       if (lpMsg[nLine]!=NULL) 
  483.       {
  484.         TextOut (hDC, 20-nHorzPos, nPos, lpMsg[nLine], lstrlen (lpMsg[nLine]));
  485.       }
  486.     }
  487.   }
  488.   EndPaint(hWnd, &ps);
  489.   return 0L;
  490. }
  491.  
  492. //*************************************************************************
  493. //*************************************************************************
  494. LRESULT CALLBACK WndMsgProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  495. {
  496.   switch (Msg)
  497.   {
  498.     case WM_CREATE: nVertPos = nHorzPos = nHorzMax = 0;
  499.                     return 0L;
  500.  
  501.     case WM_PAINT:  return PaintViewMessage (hWnd, nVertPos);
  502.  
  503.     case WM_VSCROLL: switch (wParam)
  504.       {
  505.         case SB_LINEDOWN     : if (nVertPos<(nCurMsg-(nVertSiz>>1))) nVertPos++;  break;
  506.         case SB_LINEUP       : if (nVertPos>0) --nVertPos; break;
  507.         case SB_THUMBPOSITION: nVertPos = __min ((WORD) (nCurMsg-(nVertSiz>>1)), LOWORD (lParam)); break;
  508.         case SB_PAGEUP       : nVertPos = (nVertPos>10) ? (nVertPos-10) : 0; break;
  509.         case SB_PAGEDOWN     : nVertPos = (nVertPos<(nCurMsg-nVertSiz)) ? (nVertPos+nVertSiz) : nCurMsg-(nVertSiz>>1); break;
  510.         default              : return 0L;
  511.       }
  512.       SetScrollPos (hWnd, SB_VERT, nVertPos, TRUE);
  513.       InvalidateRect (hWnd, NULL, TRUE); 
  514.       return 0L;
  515.     
  516.     case WM_HSCROLL: switch (wParam)
  517.       {
  518.         case SB_LINEDOWN     : nHorzPos = (nHorzPos<(nHorzMax-nHorzTab)) ? (nHorzPos+nHorzTab) : nHorzMax; break;
  519.         case SB_LINEUP       : nHorzPos = (nHorzPos>nHorzTab) ? (nHorzPos-nHorzTab) : 0; break;
  520.         case SB_THUMBPOSITION: nHorzPos = __min ((WORD) nHorzMax, LOWORD (lParam)); break;
  521.         case SB_PAGEUP       : nHorzPos = (nHorzPos>nHorzSiz) ? (nHorzPos-nHorzSiz) : 0; break;
  522.         case SB_PAGEDOWN     : nHorzPos = (nHorzPos<(nHorzMax-nHorzSiz)) ? (nHorzPos+nHorzSiz) : nHorzMax; break;
  523.         default              : return 0L;
  524.       }
  525.       SetScrollPos (hWnd, SB_HORZ, nHorzPos, TRUE);
  526.       InvalidateRect (hWnd, NULL, TRUE); 
  527.       return 0L;
  528.     
  529.     case WM_CLOSE  : ReleaseMsgMem();
  530.                      DestroyWindow (hWnd);
  531.                      hWndViewMsg = (HWND) NULL;
  532.                      nMaxMsg = nCurMsg = 0;
  533.                      bZoomFlag = FALSE;
  534.                      return 0;
  535.       
  536.     default        : return DefWindowProc (hWnd, Msg, wParam, lParam);
  537.   }
  538.   return 0L;
  539. }
  540.  
  541.